package com.rykj.api;

import com.rykj.AppProxy;
import com.rykj.model.entity.ResultBase;

import rx.functions.Action1;

/**
 * Description:api处理回调 author: zhangsan on 16/11/21 下午2:50.
 */

public abstract class ApiSuccessAction<T extends ResultBase> implements Action1<T> {

    @Override
    public void call(T result) {
        if (onResponse(result)) {

        } else {
            if (result.code == HttpCode.OK) {
                onSuccess(result);
            } else {
                onFail(result.code, result.message);
            }
        }

    }

    public abstract void onSuccess(T result);

    public void onFail(int erroCode, String msg) {

    }

    public boolean onResponse(T result) {
        OnErrorCodeIntercept onErrorCodeIntercept = AppProxy.INSTANCE.getOnErrorCodeIntercept();
        if (onErrorCodeIntercept == null) {
            return false;
        } else {
            return onErrorCodeIntercept.onResponse(result.code);
        }
    }
}
